﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace GameListEditor.HyperSpin
{
    class HyperSpinGameList
    {
        #region Member Variables

        /// <summary> Game list.  </summary>
        private Dictionary<String, HyperSpinGame> m_dictionary = new Dictionary<String, HyperSpinGame>();

        #endregion

        #region Properties

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Gets the items. </summary>
        /// <value> The items. </value>
        ///-------------------------------------------------------------------------------------------------
        public Dictionary<String, HyperSpinGame> Items
        {
            get { return m_dictionary; }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Gets the number of games in list. </summary>
        /// <value> The count. </value>
        ///-------------------------------------------------------------------------------------------------
        public int Count
        {
            get { return m_dictionary.Count; }
        }

        #endregion

        #region Constructors

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. </summary>
        ///-------------------------------------------------------------------------------------------------
        public HyperSpinGameList()
        {
            // Auto-initialization.
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. </summary>
        /// <param name="file"> The file to load from. </param>
        ///-------------------------------------------------------------------------------------------------
        public HyperSpinGameList(String file)
        {
            LoadXmlFile(file);
        }

        #endregion

        #region Public Methods

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Adds game. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are
        ///                                          null. </exception>
        /// <param name="game"> The HyperSpinGame to add. </param>
        ///-------------------------------------------------------------------------------------------------
        public void Add(HyperSpinGame game)
        {
            if (game == null)
                throw new ArgumentNullException("game");

            m_dictionary.Add(game.Name.ToLower(), game);
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Clears this object to its blank/initial state. </summary>
        ///-------------------------------------------------------------------------------------------------
        public void Clear()
        {
            m_dictionary.Clear();
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Loads list from specified file. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are
        ///                                          null. </exception>
        /// <exception cref="FileNotFoundException"> Thrown when the requested file is not present. </exception>
        /// <param name="file"> The file to load from. </param>
        ///-------------------------------------------------------------------------------------------------
        public void LoadXmlFile(String file)
        {
            if (file == null)
                throw new ArgumentNullException("file");

            // Make sure specified file exists.
            if (!File.Exists(file))
                throw new System.IO.FileNotFoundException("Unable to locate specified file.", file);

            // Create and load xml document.
            XmlDocument document = new XmlDocument();
            document.Load(file);
            XmlNode menuNode = document.SelectSingleNode(HyperSpinXmlConst.XML_MENU);

            Clear();

            // Load all games.
            foreach (XmlNode gameNode in menuNode.SelectNodes(HyperSpinXmlConst.XML_GAME))
                Add(new HyperSpinGame(gameNode));
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Saves list to specified file. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
        /// <exception cref="Exception">             Thrown when file does not exist. </exception>
        /// <param name="file"> The file to save to. </param>
        ///-------------------------------------------------------------------------------------------------
        public void Save(String file)
        {
            if (file == null)
                throw new ArgumentNullException("file");

            if (File.Exists(file))
            {
                try
                {
                    File.Delete(file);
                }
                catch (Exception e)
                {
                    throw new Exception("Unable to delete existing file.", e);
                }
            }

            // Create document and root node.
            XmlDocument document = new XmlDocument();
            XmlNode menuNode = document.CreateElement(HyperSpinXmlConst.XML_MENU);
            document.AppendChild(menuNode);

            // Save games.
            foreach (KeyValuePair<String, HyperSpinGame> pair in m_dictionary)
            {
                XmlNode gameNode = document.CreateElement(HyperSpinXmlConst.XML_GAME);
                pair.Value.Save(gameNode);
                menuNode.AppendChild(gameNode);
            }

            // Save to specified file.
            document.Save(file);
        }

        #endregion
    }
}
